package com.abu.ums.controller;

import com.abu.ums.pojo.Member;
import com.abu.ums.service.MemberService;
import com.abu.ums.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author 阿布
 * @since 2020-06-13
 */
@RestController
@RequestMapping("/ums/member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    @PostMapping("check")
    public void checkPhone(@RequestParam("phone") String phone) {
//           根据手机号异步发送验证码
        this.memberService.sendCode(phone);
    }

    @PostMapping("register/{code}")
    public R register(@RequestBody Member member, @PathVariable("code") String code) {
//    用户注册
        Boolean flag = this.memberService.saveMember(member, code);
        if (flag) {
            return R.ok("ok");
        }
        return R.error("fail");
    }

    @PostMapping("login")
    public Member login(@RequestParam("nickname") String nickname, @RequestParam("password") String password) {
        Member member = this.memberService.login(nickname, password);
        if (member != null) {
            return member;
        }
        return null;
    }

    @GetMapping("queryonebyid/{openId}")
    public Member getMember(@PathVariable("openId") String openId) {
        Member member = this.memberService.getOne(new QueryWrapper<Member>().eq("openid", openId));
        if (member == null) {
            return null;
        }
        return member;
    }

    @PostMapping("save")
    public R saveMember(@RequestBody Member member) {
        boolean flag = this.memberService.save(member);
        if (!flag) {
            return R.error("添加失败");
        }
        return R.ok("添加成功");
    }


}

